package com.zapi.form.annotation;
import com.zapi.form.model.dto.FormTableColumnDTO;
import com.zapi.form.model.entity.Form;
import com.zapi.form.support.request.FormOperationRequest;
import com.zapi.form.support.strategy.FormOperateManager;
import com.zapi.form.support.strategy.service.FormOperateService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.concurrent.CompletableFuture;

/**
 * SyncDataPlus注解切面
 * @author zhonglz
 * @date 2025年08月21日14:56:50
 */
@Aspect
@Component
public class DataBaseOperatePlusAspect {

    @Around("@annotation(com.zapi.form.annotation.DataBaseOperatePlus)")
    public Object handleSyncDataPlus(ProceedingJoinPoint joinPoint) throws Throwable {
        // 先执行原方法
        Object result = joinPoint.proceed();
        // 异步处理同步逻辑，不影响主业务流程
        CompletableFuture.runAsync(() -> {
            try {
                handleDataAsync(joinPoint);
            } catch (Exception e) {
                // 静默处理异常，不影响主业务
            }
        });
        return result;
    }

    private void handleDataAsync(ProceedingJoinPoint joinPoint) {
        // 获取注解参数
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        DataBaseOperatePlus dataBaseOperatePlus = method.getAnnotation(DataBaseOperatePlus.class);
        // 获取方法参数
        Object[] args = joinPoint.getArgs();
        // 通过类型匹配获取目标实体
        Object targetEntity = getEntityByType(args, dataBaseOperatePlus.targetEntityType());
        // 处理获取到的实体
        if (targetEntity != null) {
            // 在这里进行数据库操作逻辑
            processEntity(targetEntity, dataBaseOperatePlus);
        } else {
        }
    }
    /**
     * 处理实体的数据库操作
     */
    private void processEntity(Object entity, DataBaseOperatePlus annotation) {
        if (annotation == null) {
            return;
        }
        // 根据实体类型进行不同处理
        if (entity instanceof Form) {
            processFlwFormTemplate((Form) entity, annotation);
        } else {
        }
    }

    /**
     * 处理 FlwFormTemplate 的数据库操作
     */
    private void processFlwFormTemplate(Form formInfo, DataBaseOperatePlus annotation) {
        if (annotation == null) {
            return;
        }
        /// 写一个策略,分别调用对应的 新增修改删除表及字段操作
        FormOperateService dataBaseOperateService = FormOperateManager.formOperateService(annotation.operationType());
        dataBaseOperateService.executeDataBaseOperate(FormOperationRequest.builder()
                .operationType(annotation.operationType())
                .tableColumnDTO(new FormTableColumnDTO(formInfo))
                .build());
    }

    /**
     * 通过参数类型获取实体
     */
    private Object getEntityByType(Object[] args, Class<?> targetType) {
        for (Object arg : args) {
            if (arg != null && targetType.isAssignableFrom(arg.getClass())) {
                return arg;
            }
        }
        return null;
    }


} 